<?php

namespace app\modules\admin\controllers;

use app\common\service\UrlService;
use app\models\User;
use app\modules\admin\common\BaseController;

/**
 * Default controller for the `admin` module
 */
class UserController extends BaseController
{
    //登陆
    public function actionLogin()
    {
        $login_name = trim($this->post("login_name", ""));
        $login_pwd = trim($this->post("login_pwd", ""));

        if ($login_pwd == null || $login_name == null) {
            return $this->renderJs("请输入正确的用户名和密码", UrlService::buildIndexUrl("/"));
        }

        //判断用户是否存在
        $user_info = User::find()->where(["login_name" => $login_name])->one();
        if (!$user_info) {
            return $this->renderJs("请输入正确的用户名", UrlService::buildIndexUrl("/"));
        }

        //判断密码是否正确
        $auth_pwd = md5($login_pwd . md5($user_info["login_salt"]));
        if ($auth_pwd != $user_info["login_pwd"]) {
            return $this->renderJs("请输入正确的密码", UrlService::buildIndexUrl("/"));
        }

        //保存用户的登录状态
        $auth_token = $this->geneAuthToken($user_info);
        $this->setCookie("baoli_user", $auth_token . "#" . $user_info["uid"]);

        return $this->redirect(UrlService::buildAdminUrl("/dashboard/index"));
    }

    //编辑信息
    public function actionEdit()
    {

        if (\Yii::$app->request->isGet) {
            $this->layout = "main";
            return $this->render("edit", [
                'user_info' => $this->current_user
            ]);
        }
        if (\Yii::$app->request->isPost) {
            $nickname = trim($this->post('nickname', ''));
            $email = trim($this->post('email', ''));

            $user_info = $this->current_user;

            $user_info->nickname = $nickname;
            $user_info->email = $email;
            $user_info->updated_time = date("Y-m-d H:i:s");
            $user_info->update(0);

            return $this->renderJson([], "成功");
        }

    }

    //重置密码
    public function actionResetPwd()
    {
        if (\Yii::$app->request->isGet) {
            $this->layout = "main";
            return $this->render("reset_pwd", [
                'user_info' => $this->current_user
            ]);
        }

        $old_password = trim($this->post('old_password', ''));
        $new_password = trim($this->post('new_password', ''));

        $current_user = $this->current_user;

        if (!$current_user->verifyPassword($old_password)) {
            return $this->renderJSON([], "请检查原密码是否正确~~", -1);
        }

        if ($old_password == $new_password) {
            return $this->renderJSON([], "请重新输入一个吧，新密码和原密码不能相同哦！", -1);
        }

        $current_user->setPassword($new_password);
        $current_user->updated_time = date("Y-m-d H:i:s");
        $current_user->update(0);

        $this->setLoginStatus($current_user);

        return $this->renderJSON([], "修改成功~~");
    }

    //退出
    public function actionLogout()
    {
        $this->removeCookie("baoli_user");
        $this->redirect(UrlService::buildIndexUrl("/"));
    }
}
